/*
 * @Author: your name
 * @Date: 2022-04-03 16:59:51
 * @LastEditTime: 2022-04-13 19:42:13
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \drivingTow\src\trainees\trainees.service.ts
 */
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CreateTraineeDto, TraineeInfoDto, TraineeRo } from './dto/create-trainee.dto';
import { UpdateTraineeDto } from './dto/update-trainee.dto';
import { Trainee } from './entities/trainee.entity';

@Injectable()
export class TraineesService {
  constructor(
    @InjectRepository(Trainee)
    private readonly postsRepository:Repository<Trainee>
  ){}
    // 创建学员练车记录表
    async createTrainee(post:CreateTraineeDto,user):Promise<any>{
      const postParam:Partial<Trainee> = {
        ...post,
        trainee:user.id,
        traineeName:user.username,
      }
      const newPost:Trainee = await this.postsRepository.create({
        ...postParam
      });
      const created = await this.postsRepository.save(newPost)
      return created
    }

    // 学员查看练车记录
    async findById(obj):Promise<TraineeRo>{
      const qb = this.postsRepository
        .createQueryBuilder('Trainee')
        .where('Trainee.trainee =:id',{id:obj.id})
        .orderBy('Trainee.id','DESC')
      const Total = await qb.getCount();
      const {pageSize,pageNum} = obj
      qb.limit(pageSize)
      qb.offset(pageSize * (pageNum - 1))
      let Trainee = await qb.getMany();
      const result:TraineeInfoDto[] = Trainee.map((item) => item.toResponseObject());
      return {list:result,Total:Total}
    }

    // 学员查看今日预约状态
    async findByIdStatus(obj):Promise<any>{
      const qb = this.postsRepository
        .createQueryBuilder('Trainee')
        .where('Trainee.trainee =:id',{id:obj.id})
        .andWhere('Trainee.create_time =:create_time',{create_time:obj.create_time})
        .orderBy('Trainee.id','DESC')
      const Total = await qb.getCount();
      let Trainee = await qb.getMany();
      const result:TraineeInfoDto[] = Trainee.map((item) => item.toResponseObject());
      return {list:result,Total:Total}
    }

    // 修改练车状态
    async updataStatus(id):Promise<any>{
      const qb = this.postsRepository
        .createQueryBuilder('Trainee')
        .where('Trainee.id=:id')
        .setParameter('id', id);
      const result = await qb.getOne();
      await this.postsRepository.update(id, {status: 'Finish'})
      return result.toResponseObject();
    }

    // 取消练车状态
    async cancelStatus(id):Promise<any>{
      const qb = this.postsRepository
        .createQueryBuilder('Trainee')
        .where('Trainee.id=:id')
        .setParameter('id', id);
      const result = await qb.getOne();
      await this.postsRepository.update(id, {status: 'cancel'})
      return result.toResponseObject();
    }

    // 删除该练车记录
    async deleteTrainee(id):Promise<any>{
      const Trainee =await this.postsRepository.findOne(id)
      return await this.postsRepository.remove(Trainee)
    }

    // 管理员查看所有的练车记录
    async getAllTrainee(obj):Promise<TraineeRo>{
      const qb = this.postsRepository
        .createQueryBuilder('Trainee')
        .orderBy('Trainee.create_time','DESC')
        .where('1=1')
      const Total = await qb.getCount();
      const {pageSize,pageNum} = obj
      qb.limit(pageSize)
      qb.offset(pageSize * (pageNum - 1))
      let Trainee = await qb.getMany();
      const result:TraineeInfoDto[] = Trainee.map((item) => item.toResponseObject());
      return {list:result,Total:Total}
    }
}
